import { _decorator, math, Node, EventTouch, Vec2, PhysicsSystem2D, Rect, rect, UITransform, tween, Vec3, View, CCBoolean } from 'cc';
import { BaseObject } from '../../ccc3-framework-core/framework/modules/baseObject';
import { partControl } from './partControl';
const { ccclass, property } = _decorator;

@ccclass('partHorizontalControl')
export class partHorizontalControl extends BaseObject {

    @property(Node)
    mapNode: Node = null;
    @property(Node)
    bgNode: Node = null;

    @property({
        type: CCBoolean,
        tooltip: '是否能横向切换',
        displayName: '是否能横向切换'
    })
    public isHorizontal: boolean = false;

    @property({
        type: CCBoolean,
        tooltip: '是否能竖向切换',
        displayName: '是否能竖向切换'
    })
    public isVertical: boolean = true;

    private containerTrans: UITransform;
    start() {
        this.bindTouchStart(this.node, this.onTouchStart)
        this.bindTouchMove(this.node, this.onTouchMove)
        this.bindTouchEnd(this.node, this.onTouchEnd)
        this.containerTrans = this.node.getComponent(UITransform)
    }
    private isMoving: boolean = false;
    private startPos: Vec2 = null;
    private onTouchStart(e: EventTouch) {

        this.startPos = e.getUILocation();
        console.log("this.startPos", this.startPos)
        let finger1 = this.node.parent.getChildByName("finger1");
        finger1 && (finger1.active = false);
        let finger2 = this.node.parent.getChildByName("finger2");
        finger2 && (finger2.active = true);
    }
    private onTouchMove(e: EventTouch) {

    }
    private onTouchEnd(e: EventTouch) {
        if (this.isMoving) return;
        let endPos = e.getUILocation();
        console.log("this.endPos", endPos)
        let dis = endPos.subtract(this.startPos);
        let localStartPos = this.containerTrans.convertToNodeSpaceAR(new Vec3(this.startPos.x, this.startPos.y, 0))
        let localEndPos = this.containerTrans.convertToNodeSpaceAR(new Vec3(endPos.x, endPos.y, 0))
        let startIndex = this.getTouchInChildrenIndex(new Vec2(localStartPos.x, localStartPos.y));
        if (startIndex == -1) return;

        let fromNode = this.mapNode.children[startIndex];
        let trans = fromNode.getComponent(UITransform);
        if (this.isHorizontal && Math.abs(dis.x) > trans.width / 2) {
            this.moveToMap(dis.x < 0, startIndex);
        }
        else if (this.isVertical && Math.abs(dis.y) > 50) {
            let fromNode = this.mapNode.children[startIndex];
            let partCtl = fromNode.getComponent(partControl);
            partCtl.startPos = this.startPos;
            partCtl.onTouchEnd(e);
        }
    }

    private getTouchInChildrenIndex(point: Vec2): number {
        let index = -1;
        let children = this.mapNode.children;
        for (let i = 0; i < children.length; i++) {
            let child = children[i];
            let childTrans = child.getComponent(UITransform);
            if (child.position.x + childTrans.width / 2 > point.x && child.position.x - childTrans.width / 2 < point.x) {
                index = i;
                break;
            }
        }
        return index;
    }


    private moveToMap(isLeft: boolean, index: number) {
        let tweenDuration: number = 1.0;
        let fromNode = this.mapNode.children[index];
        let bgNode = this.bgNode.children[index];
        let toNode: Node = null;
        let bgToNode: Node = null;
        if (isLeft) {
            if (index > 0) {
                toNode = this.mapNode.children[index - 1];
                bgToNode = this.bgNode.children[index - 1];
            }
            else
                return;
        }
        else {
            if (index < this.mapNode.children.length - 1) {
                toNode = this.mapNode.children[index + 1];
                bgToNode = this.bgNode.children[index + 1];
            }
            else
                return;
        }
        this.isMoving = true;

        let toNodePos = toNode.position.clone();
        let toNodePos2 = toNode.position.clone();
        let fromNodePos = fromNode.position.clone();
        let fromNodePos2 = fromNode.position.clone();
        let fromSiblingIndex = fromNode.getSiblingIndex();
        let fromSiblingIndex2 = bgNode.getSiblingIndex();
        let toSiblingIndex = toNode.getSiblingIndex();
        let toSiblingIndex2 = bgToNode.getSiblingIndex();

        tween(fromNode).to(tweenDuration, { position: toNodePos }).start();
        tween(bgNode).to(tweenDuration, { position: toNodePos2 }).start();

        tween(toNode).to(tweenDuration, { position: fromNodePos }).call(() => {
            toNode.setSiblingIndex(fromSiblingIndex);
            fromNode.setSiblingIndex(toSiblingIndex);
            this.isMoving = false;
        }).start();
        tween(bgToNode).to(tweenDuration, { position: fromNodePos2 }).call(() => {
            bgToNode.setSiblingIndex(fromSiblingIndex2);
            bgNode.setSiblingIndex(toSiblingIndex2);
            this.isMoving = false;
        }).start();
    }

    update(deltaTime: number) {

    }
}

